{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'{\"returnPhrase\":[\"\\xe4\\xbd\\xa0\\xe5\\xa5\\xbd\"],\"query\":\"\\xe4\\xbd\\xa0\\xe5\\xa5\\xbd\",\"errorCode\":\"0\",\"l\":\"zh-CHS2en\",\"tSpeakUrl\":\"http://openapi.youdao.com/ttsapi?q=hello&langType=en&sign=4C76EB6FF81B7E0E6F0B54AEE257856C&salt=1611471732351&voice=4&format=mp3&appKey=6fa280d0332fdbff&ttsVoiceStrict=false\",\"web\":[{\"value\":[\"Hello\",\"How do you do\",\"hi\"],\"key\":\"\\xe4\\xbd\\xa0\\xe5\\xa5\\xbd\"},{\"value\":[\"How are you\",\"How Do You Do\",\"Are You OK\"],\"key\":\"\\xe4\\xbd\\xa0\\xe5\\xa5\\xbd\\xe5\\x90\\x97\"},{\"value\":[\"Hey Cortana\"],\"key\":\"\\xe4\\xbd\\xa0\\xe5\\xa5\\xbd\\xe5\\xb0\\x8f\\xe5\\xa8\\x9c\"}],\"requestId\":\"e2c106fd-8f99-48c3-a39f-66c734867e55\",\"translation\":[\"hello\"],\"dict\":{\"url\":\"yddict://m.youdao.com/dict?le=eng&q=%E4%BD%A0%E5%A5%BD\"},\"webdict\":{\"url\":\"http://m.youdao.com/dict?le=eng&q=%E4%BD%A0%E5%A5%BD\"},\"basic\":{\"phonetic\":\"n\\xc7\\x90 h\\xc7\\x8eo\",\"explains\":[\"hello\",\"hi\",\"how do you do\"]},\"isWord\":true,\"speakUrl\":\"http://openapi.youdao.com/ttsapi?q=%E4%BD%A0%E5%A5%BD&langType=zh-CHS&sign=1036EF1DE682ADEBC1491F6E13CEA1AF&salt=1611471732351&voice=4&format=mp3&appKey=6fa280d0332fdbff&ttsVoiceStrict=false\"}'\n"
     ]
    }
   ],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "import sys\n",
    "import uuid\n",
    "import requests\n",
    "import hashlib\n",
    "import time\n",
    "from imp import reload\n",
    "\n",
    "import time\n",
    "\n",
    "reload(sys)\n",
    "\n",
    "YOUDAO_URL = 'https://openapi.youdao.com/api'\n",
    "APP_KEY = '6fa280d0332fdbff'\n",
    "APP_SECRET = '0v0qAvcZezQXx6VMWjty8aimdKqZDpFF'\n",
    "\n",
    "\n",
    "def encrypt(signStr):\n",
    "    hash_algorithm = hashlib.sha256()\n",
    "    hash_algorithm.update(signStr.encode('utf-8'))\n",
    "    return hash_algorithm.hexdigest()\n",
    "\n",
    "\n",
    "def truncate(q):\n",
    "    if q is None:\n",
    "        return None\n",
    "    size = len(q)\n",
    "    return q if size <= 20 else q[0:10] + str(size) + q[size - 10:size]\n",
    "\n",
    "\n",
    "def do_request(data):\n",
    "    headers = {'Content-Type': 'application/x-www-form-urlencoded'}\n",
    "    return requests.post(YOUDAO_URL, data=data, headers=headers)\n",
    "\n",
    "\n",
    "def connect():\n",
    "    q = \"你好\"\n",
    "\n",
    "    data = {}\n",
    "    data['from'] = 'zh-CHS'\n",
    "    data['to'] = 'en'\n",
    "    data['signType'] = 'v3'\n",
    "    curtime = str(int(time.time()))\n",
    "    data['curtime'] = curtime\n",
    "    salt = str(uuid.uuid1())\n",
    "    signStr = APP_KEY + truncate(q) + salt + curtime + APP_SECRET\n",
    "    sign = encrypt(signStr)\n",
    "    data['appKey'] = APP_KEY\n",
    "    data['q'] = q\n",
    "    data['salt'] = salt\n",
    "    data['sign'] = sign\n",
    "    data['vocabId'] = \"您的用户词表ID\"\n",
    "\n",
    "    response = do_request(data)\n",
    "    contentType = response.headers['Content-Type']\n",
    "    if contentType == \"audio/mp3\":\n",
    "        millis = int(round(time.time() * 1000))\n",
    "        filePath = \"合成的音频存储路径\" + str(millis) + \".mp3\"\n",
    "        fo = open(filePath, 'wb')\n",
    "        fo.write(response.content)\n",
    "        fo.close()\n",
    "    else:\n",
    "        print(response.content)\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    connect()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
